package com.devin.controller;

import com.alibaba.fastjson.JSON;
import com.devin.VParty;
import com.devin.model.constant.VPartyConst;
import com.devin.model.dto.JsonResult;
import com.devin.model.dto.RoomDto;
import com.devin.model.entity.Movie;
import com.devin.model.entity.Room;
import com.devin.model.entity.User;
import com.devin.model.vo.RoomVo;
import com.devin.repository.MovieRepository;
import com.devin.repository.RoomRepository;
import com.devin.repository.UserRepository;
import com.devin.service.MovieService;
import com.devin.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;
import sun.swing.BakedArrayList;

import javax.servlet.http.HttpSession;
import javax.xml.ws.Response;
import java.util.*;

import static org.springframework.data.domain.Sort.Direction.DESC;


/**
 *
 *  @author devin
 *  @date 2020-04-13 21:25
 *  @version 1.0.2
 */



@CrossOrigin(origins = "*")
@ResponseBody
@Slf4j
@Controller
public class Index {


//    @Autowired
//    private RoomRepository roomRepository;
//
//    @Autowired
//    private UserRepository userRepository;
//
//    @Autowired
//    private RedisTemplate<String,Object> redisTemplate;
//
//    @Autowired
//    private LiveService liveService;
//
//    @Autowired
//    private RedisUtil redisUtil;
//
//    @Autowired
//    private MovieRepository movieRepository;
//
//    @Autowired
//    private MovieService movieService;



    @RequestMapping("/")
    public String index(){
        log.info("hello world");
        return "hello world";
    }


    @RequestMapping("/index")
    public String getIndex(){
        return "/front-end/user_login";
    }


//    @ResponseBody
//    @RequestMapping("/redis")
//    public String getRedis(){
//        List<User> list = new ArrayList<>(5);
//        redisUtil.set("user_list",list);
//        return "redis";
//    }

//    @ResponseBody
//    @RequestMapping("/mysql")
//    public JsonResult getMysql(){
//        final List<Movie> movies = movieRepository.findAll();
//       return new JsonResult(1,"success",movies);
//    }


    //用户登入验证
//    @RequestMapping("/login")
//    public JsonResult getLogin(String username,String password){
//
//        User user = userRepository.findByUsername(username);
//        if(user == null)
//        {
//            userRepository.save(new User(username,password));
//            return new JsonResult(1,"成功注册");
//        }
//        else{
//            if(!user.getPassword().equals(password)){
//                return new JsonResult(0,"密码错误");
//            }else{
//                return new JsonResult(1,"登入成功");
//            }
//        }
//    }


    //电影列表
//    @RequestMapping("/movieList")
//    public Page<Movie> getMovieList(@PageableDefault(size = 6,page = 0,sort = "createDate")Pageable pageable)
//    {
//        Page<Movie> movies = movieService.listAll(pageable);
//        return movies;
//    }

    //房间列表
//    @RequestMapping("/roomList")
//    public Page<Room> getRoomList(@PageableDefault(size = 8,page = 0,sort = "createTime")Pageable pageable)
//    {
////        movieRepository.findAll(new PageRequest(page, count) );
//        Page<Room> rooms = roomRepository.findAll(pageable);
//        return rooms;
//    }



    //直播列表
//    @RequestMapping("/liveList")
//    public Page<Live> getLiveList(@PageableDefault(size = 8,page = 0,sort = "id")Pageable pageable){
//        log.info("page:");
//
//        log.info(String.valueOf(pageable.getPageNumber()));
//
//        Page<Live> lives = liveService.listAll(pageable);
//        return lives;
//    }




//    @RequestMapping("/getMovieById")
//    public Movie getMovieById(Long id){
//        log.info("getMovieById");
//        Optional<Movie> movie = this.movieRepository.findById(id);
//        return movie.get();
//    }

//    @RequestMapping("/enterLive")
//    public JsonResult enterLive(Long )



//    @ApiOperation(value = "创建房间")
//    @RequestMapping("/create")
//    public JsonResult   createRoom(RoomDto roomDto, HttpSession session){
//
//        //把房间加入房间列表
//        log.info("roomDto.toString()" + roomDto.toString());
//        Optional<Movie> movie = movieRepository.findById(roomDto.getMovieId());
//
//        //插入房间 mysql
//        String username = (String) session.getAttribute("username");
//        Room room = new Room(roomDto.getRoomname(),roomDto.getVideoname(),roomDto.getMovieId(),new Date(), username,1,movie.get().getFaceUrl(),"movie");
//        Room save = roomRepository.save(room);
//        // redis把房间的人加入房间id
//        List<String> userlist = new ArrayList<>(5);
//        userlist.add(username);
//        boolean rediscode = redisUtil.set(save.getId().toString(), userlist);
//
//        //看成功与否返回
//        HashMap<String,Object> hashMap = new HashMap<String,Object>();
//        if(save == null || rediscode == false){
//            hashMap.put("code","error");
//        }else {
//            //success  把 房间返回回来
//            hashMap.put("roomId",save.getId());
//            hashMap.put("code","success");
//        }
//        log.info("save:" + save.toString());
//        log.info("roomId:" + save.getId());
//        return new JsonResult(JSON.toJSON(hashMap));
//    }

    //搜索电影
//    @RequestMapping("/search")
//    public Page<Movie> search(String key, @PageableDefault(size = 6,page = 0)Pageable pageable){
//        log.info(key);
//        Page<Movie> search = movieRepository.search(key,pageable);
//        return search;
//    }


//    @RequestMapping("/enterRoom")
//    public JsonResult enterRoom(Long roomId,HttpSession httpSession){
//        log.info("enter roomid" + roomId);
//        //得到房间的名字
//        String username = (String) httpSession.getAttribute("username");
//        Optional<Room> byId = roomRepository.findById(roomId);
//        byId.get().setCurrentPeople(byId.get().getCurrentPeople() + 1);
//        roomRepository.save(byId.get());
//        List<String> userlist = (List<String>) redisUtil.get(roomId.toString());
//        userlist.add(username);
//        redisUtil.set(roomId.toString(), userlist);
//        return new JsonResult(JSON.toJSON(byId.get()));
//    }

//    @RequestMapping("/getRoomById")
//    public JsonResult getRoomById(Long roomId){
//        Optional<Room> byId = roomRepository.findById(roomId);
//        return new JsonResult(byId.get());
//    }

}
